package xyz.boot.ssm.modules.mybatis.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import xyz.boot.ssm.modules.mybatis.dao.LogDao;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class LogService {

    @Resource
    LogDao dao;

    public void insert(){
        dao.insert1();
    }

    public void insert(String log){
        dao.insert2(log,new Date());
    }

    public void insert3(String log){
        dao.insert1();
        dao.insert2(log,new Date());
    }
    public void insert4(){
        dao.insert2("4-1",new Date());
        int i = 1/0;
        dao.insert2("4-2",new Date());
    }
    @Transactional
    public void insert5(){
        dao.insert2("4-1",new Date());
        int i = 1/0;
        dao.insert2("4-2",new Date());
    }

    public void insertSteps() throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            dao.insert1();
            Thread.sleep(1000);
        }
    }


    @Transactional()
    public void insert6(){
        try {

            dao.insert2("4-1",new Date());
            int i = 1/0;
            dao.insert2("4-2",new Date());
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new IllegalArgumentException("insert6");
        }
    }

}
